package controller.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import model.domain.Priority;

public class PriorityConverter implements Converter  {

    public PriorityConverter(){

    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value)
    {

        if (value == null || (value.trim().length() == 0))
        {
            return value;
        }

        Priority priority = new Priority();

        String[] splitedString = value.split(" - ");
        priority.setPriorityID(Integer.parseInt(splitedString[0]));
        priority.setDescription(splitedString[1]);

        return priority;
    }

    @Override
      public String getAsString(FacesContext context, UIComponent component, Object value)
      {

       Priority priority = null;

        if (value instanceof Priority)
        {
            priority = (Priority)value;

            StringBuilder priorityAsString = new StringBuilder();
            priorityAsString.append(priority.getPriorityID());
            priorityAsString.append(" - ");
            priorityAsString.append(priority.getDescription());
            String string = priorityAsString.toString();

            return string;
        }
        return "";
    }

}
